package com.xpgk.mvc.collection.bentryRegister;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.baseInfoSet.BaseInfoSet;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.tools.ToolSqlXml;

import net.sf.json.JSONObject;

public class CollectionBentryRegisterController extends BaseController {

	private CollectionBentryRegisterService collectionBentryRegisterService = new CollectionBentryRegisterService();

	// http://127.0.0.1:8080/shouba2.0/bentryRegister?cusIds=4079464587104234a8fd8a4f5a399fd2
	public void index() {
		String cusIds = getPara("cusIds");
		if (cusIds != null) {
			CollectionCustomer cus = CollectionCustomer.dao.findById(cusIds);
			setAttr("customer", cus);
			
			
			CollectionCustomerAgreement cusagr = cus.getCustomerAgreement();
			Product p = Product.dao.findById(cusagr.getStr(CollectionCustomerAgreement.column_codeIds));
			if (p != null && p.getStr(Product.column_proUnits).equals(Code.dlc_units)) {
				setAttr("isdlc", "1");
			}
		}

		List<Record> find = Db
				.find("SELECT ids,`name` from sb_product WHERE typeIds='455018290a8f4afbbaf29c61904d6406'");
		setAttr("product", find);
		setAttr("organids", getCUserOrganIds());
		render("collectionBentryRegister.html");
	}
	
	public void getCus() {
		String cusIds = getPara("cusIds");
		if (cusIds != null) {
			CollectionCustomer cus = CollectionCustomer.dao.findById(cusIds);
			setAttr("customer", cus);
			CollectionCustomerAgreement cusagr = cus.getCustomerAgreement();
			Product p = Product.dao.findById(cusagr.getStr(CollectionCustomerAgreement.column_codeIds));
			if (p != null && p.getStr(Product.column_proUnits).equals(Code.dlc_units)) {
				setAttr("isdlc", "1");
			}
		}

		List<Record> find = Db
				.find("SELECT ids,`name` from sb_product WHERE typeIds='455018290a8f4afbbaf29c61904d6406'");
		setAttr("product", find);
		setAttr("organids", getCUserOrganIds());
		render("/collection/bentryRegister/collectionBentryRegister.html");
	}

	/**
	 * 获取疗程
	 * 
	 */
	public void getLc() {
		String sql = getSqlMy(Product.sqlId_getProductsByType);
		List<Product> pList = Product.dao.find(sql, Code.lc_ids);
		renderJson(pList);
	}
	
	/**
	 * 获取疗程
	 * 
	 */
	public void updateTreatment() {
		String cusIds = getPara("customerIds");
		String codeIds = getPara("customerAgreement.codeIds");//疗程id
		Integer treatment = Integer.parseInt(getPara("customerAgreement.treatment")) ;//疗程数
		
		List<CollectionCustomerAgreement> cas = CollectionCustomerAgreement.dao.find("SELECT * from sb_customer_agreement WHERE customerIds=? order by createDate desc",cusIds);
		CollectionCustomerAgreement ca= cas.get(0);
		ca.set(CollectionCustomerAgreement.column_codeIds, codeIds);
		if(codeIds.equals("27aeb2d41d044394b9b58f6814baa4fa")){//一
			ca.set(CollectionCustomerAgreement.column_cost, 760);
			ca.set(CollectionCustomerAgreement.column_treatment, 1);
		}else if(codeIds.equals("0896ce4d4970455d9184e6dd1ced6a0e")){//标准
			ca.set(CollectionCustomerAgreement.column_cost, 380*treatment);
			ca.set(CollectionCustomerAgreement.column_treatment, treatment);
		}else if(codeIds.equals("4777c2c215b6408ab13c1eac4190ce46")){//单
			ca.set(CollectionCustomerAgreement.column_cost, 680);
			ca.set(CollectionCustomerAgreement.column_treatment, 1);
		}
		ca.update();
		renderJson("200");
	}

	/**
	 * 获取一疗程
	 * 
	 */
	public void getYlc() {
		String sql = getSqlMy(Product.sqlId_ylc);
		Product p = Product.dao.findFirst(sql, Code.lc_ids, Code.bzlc_units, Code.ylc_capacity);
		if (p != null) {
			renderText(p.getStr(Product.column_ids));
		} else {
			renderText("");
		}
	}

	/**
	 * 获取消费金额
	 * 
	 */
	public void getCost() {
		// String delWeight=getPara("delWeight");
		String tem = getPara("tem");
		String productIds = getPara("productIds");
		// 获取当前登录机构id
		String orgids = getCUserOrganIds();
		RenderBean result = collectionBentryRegisterService.getCost(tem, productIds, orgids);
		renderJson(result);
	}

	/**
	 * 协议预览（弹窗）
	 */
	public void showAgree() {
		String cusIds = getPara("customerIds");
		CollectionCustomer cus = CollectionCustomer.dao.findById(cusIds);
		setAttr("customer", cus);
		render("/collection/bentryRegister/collectionCustomerAgreement.html");
	}

	/**
	 * 基础资料（弹窗）
	 */
	public void showInfo() {
		String cusIds = getPara("customerIds");
		setAttr("customerIds", cusIds);
		List<BaseInfoSet> customerInfos = BaseInfoSet.dao.getChildByCustomer(cusIds);
		if (customerInfos.size() > 0) {
			setAttr("customerInfos", customerInfos);
		}
		setAttr("baseInfo", BaseInfoSet.dao.findById(BaseInfoSet.rootIds));
		render("/collection/bentryRegister/collectionCustomerDetail.html");
	}

	/**
	 * 顾客产品（弹窗）
	 */
	public void customerProduct() {
		// String cusIds=getPara("customerIds");
		// Customer cus=Customer.dao.findById(cusIds);
		setAttr("customerIds", getPara("customerIds"));
		render("/customer/customerProdut.html");
	}

	public void getCusPro() {
		String cusIds = getPara("customerIds");
		String orgids = getPara("organIds", getCUserOrganIds());
		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("orgids", orgids);
		queryParam.put("customerID", cusIds);
		LinkedList<Object> paramValue = new LinkedList<Object>();
		String sql = ToolSqlXml.getSqlMy(CollectionCustomerProduct.sqlId_selByCusAndOrg, queryParam,
				ConstantRender.sql_renderType_beetl, paramValue);
		List<Record> cpList = Db.find(sql, paramValue.toArray());
		renderJson(cpList);
	}

	/**
	 * 保存基础资料
	 */

	public void saveBaseInfo(){

		String organIds = getCUserOrganIds();
		String userIds = getCUserIds();
		CollectionCustomerHabit customerHabit = getModel(CollectionCustomerHabit.class,"customerHabit");
		
		RenderBean result = collectionBentryRegisterService.saveBaseInfo(customerHabit,organIds,userIds);
		renderJson(result.getMap());

	}

	/**
	 * 保存顾客登记信息
	 */
	@Before(Tx.class)
	public void saveCus() {
		CollectionCustomer cus = getModel(CollectionCustomer.class, "customer");
		CollectionCustomerDetail cusDet = getModel(CollectionCustomerDetail.class, "customerDetail");
		CollectionCustomerAgreement cusAgr = getModel(CollectionCustomerAgreement.class);
		// 获取当前登录机构id
		String orgids = getCUserOrganIds();
		RenderBean result = collectionBentryRegisterService.saveCus(cus, cusDet, cusAgr, orgids);
		renderJson(result.getMap());
	}

	/**
	 * 图片上传
	 */
	public void imageUpload() {
		List<UploadFile> files = getFiles();
		// UploadFile uploadFile=getFile("zmfile");
		String cusIds = getPara("customerIds");
		JSONObject json = collectionBentryRegisterService.imageUpload(files, cusIds);
		renderJson(json);
	}

	/**
	 * 获取code child
	 * 
	 */
	public void getChild() {
		System.out.println("............................");
		String codeIds = getPara("codeIds");
		String sql = getSqlMy(Code.sqlId_child);
		List<Code> cList = Code.dao.find(sql, codeIds);
		renderJson(cList);
	}

	/**
	 * 根据会员编号,手机号,姓名查出会员信息
	 * 
	 */
	public void getCusByHybh() {
		String hybh = getPara("hybh");
		String name = getPara("name");
		String phone = getPara("phone");
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		Map<String, Object> mm = collectionBentryRegisterService.getCusByHybh(hybh, name, phone, orgid);
		renderJson(mm);
	}
	
	
	/**
	 * 获取会员名字/id
	 * 
	 */
	public void getCusInfo() {
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		List<CollectionCustomer> mm = collectionBentryRegisterService.getCusInfo(orgid);
		renderJson(mm);
	}

	/**
	 * 上一个下一个
	 * 
	 */
	public void selOther() {
		String hybh = getPara("hybh");
		String obj = getPara("obj");
		// 获取当前机构id
		String organids = getCUserOrganIds();
		CollectionCustomer cus = collectionBentryRegisterService.selOther(hybh, obj, organids);
		if (cus != null) {
			CollectionCustomerAgreement cusagr = cus.getCustomerAgreement();
			Product p = Product.dao.findById(cusagr.getStr(CollectionCustomerAgreement.column_codeIds));
			if (p != null && p.getStr(Product.column_proUnits).equals(Code.dlc_units)) {
				setAttr("isdlc", "1");
			}
		}
		setAttr("customer", cus);
		setAttr("organids", organids);
		if (cus != null) {
			setAttr("chargePersion", cus.getCustomerDetail().getStr(CollectionCustomerDetail.column_chargePersion));
		}
		render("/collection/bentryRegister/collectionBentryRegister.html");
	}

	/**
	 * 获取顾客详情
	 */
	public void selCustomerInfo() {
		String customerIds = getPara("customerIds");
		CollectionCustomer cus = CollectionCustomer.dao.findById(customerIds);
		if (cus != null) {
			CollectionCustomerDetail cd = cus.getCustomerDetail();
			float sc_weight = cd.getFloat(CollectionCustomerDetail.column_firstWeight);
			float now_weight = cd.getFloat(CollectionCustomerDetail.column_nowWeight);
			setAttr("yijianWeight", sc_weight - now_weight);
			float del = cd.getFloat(CollectionCustomerDetail.column_delWeight);
			// 创建一个数值格式化对象
			NumberFormat numberFormat = NumberFormat.getInstance();
			// 设置精确到小数点后2位
			numberFormat.setMaximumFractionDigits(2);

			String wcbl = numberFormat.format((sc_weight - now_weight) / del * 100);

			CustomerService cs = CollectionCustomer.dao.getCustomerServiceCount(customerIds, getCUserOrganIds());
			long count = cs.getLong("count");
			setAttr("count", count);
			setAttr("wcbl", wcbl + "%");
			setAttr("customer", cus);
		}
		render("/customer/customerInfoUl.html");
	}

}
